package level02.one;

import java.io.*;
import java.util.Scanner;

/**
 * 删除图书：根据图书编号查找图书，找到后先提示是否删除，如
 * 果用户选择”是”，执行删除。同时文件中的数据也要被删除；
 */
public class Drop {
    public static void drop(File file) throws Exception {
        System.out.println("请选择你要删除的图书编号");
        Scanner scanner = new Scanner(System.in);
        String nextLine = scanner.nextLine();

        //读取文件中的信息
        BufferedReader bufferedReader = new BufferedReader(new FileReader(file));

        //创建一个stringBuilder用来接受读取到的信息
        StringBuilder stringBuilder = new StringBuilder();

        String line;
        while ((line = bufferedReader.readLine()) != null) {

            String[] split = line.split(",");

            if (split[0].equals(nextLine)) {
                System.out.println("该图书信息为:" + line);
                System.out.println("你确定要删除吗(是/否)");
                String nextLine1 = scanner.nextLine();

                switch (nextLine1) {
                    case "是":
                        System.out.println("删除成功");
                        break;

                    case "否":
                        //把修改后的内容写入stringBuilder中
                        stringBuilder.append(line);
                        stringBuilder.append("\r\n");
                        System.out.println("取消删除");
                        break;

                    default:
                        System.out.println("请输入--是/否");
                }

            } else {
                stringBuilder.append(line).append("\r\n");
            }

        }
        //写入文件中
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file));
        bufferedWriter.write(stringBuilder.toString());
        bufferedWriter.close();
    }
}
